﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;



namespace SR1000
{
	public class ScanEventArgs : EventArgs
	{

		public ScanEventArgs() : base() { }



		public string Message { get; set; }

	}
	public class Scan
	{
		private bool _isOpen = false;
		//public int ComTestErrCnt { get; set; } = 0;
		//public int ComMarkErrCnt { get; set; } = 0;
		//private string markSendStr = "";
		//public string Mb = "";
		//public string Txt1 = "";
		//public string Txt2 = "";
		//public string Txt3 = "";
		//public string Txt4 = "";
		//public string Dm = "";

		//public bool IsTestComplete { get; set; } = false;
		//public bool IsMarkComplete { get; set; } = false;

		public bool IsOpen
		{
			get
			{
				
				//if (m_reader.LastErrorInfo == ErrorCode.OpenFailed)
				//{
				//	_isOpen = false;
				//}
				return _isOpen;
			}
			set
			{ }
		}

		//Action<byte[]> ReBackFun = new Action<byte[]>(San_RecvDat);
		public string LastErrorString
		{
			get
			{
				return Enum.GetName(typeof(ErrorCode), m_reader.LastErrorInfo);
			}
			set { }


		}
		public Scan(string IpAddr, int Port = 5000)
		{
			m_reader.IpAddress = IpAddr;
			m_reader.DataPort = Port;
			_isOpen = m_reader.Connect((data) =>
			{
			//BeginInvoke(new delegateUserControl(ScanReceivedData), Encoding.ASCII.GetString(data));
			RaiseTestEvent(Encoding.ASCII.GetString(data));
			});
		}

		public string GetIpAddr()
		{
			return m_reader.IpAddress;
		}
		public int GetPort()
		{
			return m_reader.DataPort;
		}

		public bool Open()
		{
			//_isOpen = m_reader.Connect((data) =>
			//{
			//	//BeginInvoke(new delegateUserControl(ScanReceivedData), Encoding.ASCII.GetString(data));
			//	San_RecvDat(data);
			//});
			_isOpen = m_reader.Connect(San_RecvDat);
			return _isOpen;
		}

		public bool Close()
		{
			m_reader.Disconnect();
			return _isOpen;
		}
		public string SendCmd(string cmd="LON")
		{
			return m_reader.ExecCommand(cmd);
		}

		public string CopyImages()
		{
			string returnstr = "ERROR.JPG";

			return returnstr;
		}

		private void San_RecvDat(byte[] data)
		{
			string strdat = Encoding.ASCII.GetString(data);
			RaiseTestEvent(strdat);
			//if (te.Message.StartsWith("<Etest>"))
			//{
			//    ComTestErrCnt = 0;
			//    IsTestComplete = true;
			//    if (te.Message.StartsWith("<Etest><" + Mb + ">"))
			//    {
			//        ComMarkErrCnt = 0;
			//        IsMarkComplete = true;
			//        RaiseTestEvent("data");
			//    }
			//    else
			//    {
			//        RaiseTestEvent("test");
			//    }

			//}
			//else if (te.Message == "<XE>")
			//{
			//    RaiseTestEvent("printEnd");
			//}

		}


		//public void TestCmd()
		//{
		//    if (IsTestComplete == false && ComTestErrCnt < 1000) ComTestErrCnt++;
		//    IsTestComplete = false;
		//    SendCmd("<Etest>");
		//}
		//public void MarkCmd()
		//{
		//    if (IsMarkComplete == false && ComMarkErrCnt < 1000) ComMarkErrCnt++;
		//    IsMarkComplete = false;
		//    markSendStr = "<Etest><L" + Mb + "><Dtxt1," + Txt1 + "><Dtxt2," + Txt2 + "><Dtxt3," + Txt3 + "><Dtxt4," + Txt4 + "><Ddm," + Dm + ">";
		//    SendCmd(markSendStr);
		//}


		//public void SendCmd(string cmd)
		//{
		//    if (_isOpen) serial.Send(cmd);
		//}



		// 这个方法引发事件

		public void RaiseTestEvent(string message)

		{

			if (CmdRecv == null) return;

			CmdRecv(this, new ScanEventArgs { Message = message });

		}

		public event EventHandler CmdRecv;

	}
}
